package com.hengda.museumonline.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseLoadMoreHeaderAdapter;
import com.hengda.museumonline.adapter.BaseViewHolder;
import com.hengda.museumonline.adapter.ErrorAdapter;
import com.hengda.museumonline.entity.CommentToBean;
import com.hengda.museumonline.entity.DomesticBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.ui.NewsContentActivity;
import com.hengda.museumonline.utils.SharedPreferenceUtil;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.List;

/**
 * @author：JFWU on 2016/9/26 18:58
 * @E-mail：jeffy12138@126.com
 */
public class CommentToFragment extends Fragment {
    private LinearLayout fl_content;
    private RecyclerView recyclerView;
    private SharedPreferenceUtil spUtil;
    private CommentToBean commentToBean1 = new CommentToBean();
    private CommentToBean commentToBean2 = new CommentToBean();
    private int i = 1;
    private MyAdapter mAdapter;

    public CommentToFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        spUtil = new SharedPreferenceUtil(getContext(), "com.hengda.museumonline");
        fl_content = (LinearLayout) inflater.inflate(R.layout.fragment_comment, container, false);
        recyclerView = (RecyclerView) fl_content.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        getDataFromServer();
        return fl_content;
    }

    private void getDataFromServer() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "comment/indexs", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(getContext()).versionName);
        request.add("token", spUtil.getToken());
        request.add("offset", 0);
        request.add("size", 10);
        CallServer.getRequestInstance().add(getContext(), 0, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response response) {
            int responseCode = response.getHeaders().getResponseCode();// 服务器响应码
            Gson gson = new Gson();
            if (0 == what) {
                System.out.println("结果：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + response.get().toString());
                commentToBean1 = gson.fromJson(response.get().toString(), CommentToBean.class);
                if (commentToBean1 != null && commentToBean1.getData() != null && commentToBean1.getData().size() > 0) {
                    mAdapter = new MyAdapter(getContext(), recyclerView, commentToBean1.getData(), R.layout.commentto_item);
                    recyclerView.setAdapter(mAdapter);
                    mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
                        @Override
                        public void onLoadMore() {
                            Request request = NoHttp.createStringRequest(MyApplication.url + "comment/indexs", RequestMethod.GET);
                            request.add("p", "a");
                            request.add("v", PackageUtil.getAppPackageInfo(getContext()).versionName);
                            request.add("token", spUtil.getToken());
                            request.add("offset", 10 * i);
                            request.add("size", 10);
                            i += 1;
                            CallServer.getRequestInstance().add(getContext(), 1, request, listener);
                        }
                    });
                }
            } else if (1 == what) {
                commentToBean2 = gson.fromJson(response.get().toString(), CommentToBean.class);
                if (commentToBean2.getData() == null) {
                    mAdapter.setLoading(false);
                } else {
                    mAdapter.addAll(commentToBean2.getData());
                }
            }
        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
            System.out.println("失败：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + exception);
            recyclerView.setAdapter(new ErrorAdapter(getContext()));
        }

        @Override
        public void onFinish(int what) {

        }
    };

    class MyAdapter extends BaseLoadMoreHeaderAdapter<CommentToBean.DataBean> {

        public MyAdapter(Context mContext, RecyclerView recyclerView, List<CommentToBean.DataBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(final Context mContext, RecyclerView.ViewHolder holder, final CommentToBean.DataBean dataBean) {
            ((BaseViewHolder) holder).setText(R.id.content, dataBean.getContent());
            ((BaseViewHolder) holder).setText(R.id.createtime, dataBean.getCreatetime().substring(5,16));
            ((BaseViewHolder) holder).setText(R.id.post_title, "原文：" + dataBean.getPost_title());
            ((BaseViewHolder) holder).setCircularImageUrl(R.id.avatar, dataBean.getAvatar(), R.drawable.setting_image);
        }
    }


}
